package org.RSG.Interface;

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

import org.RSG.Application.*;

public class CChooser extends JComponent implements MouseListener
{
	private static final long serialVersionUID = 1L;

	public CChooser()
	{
		this.setPreferredSize(new Dimension(50, 30));
		this.addMouseListener(this);
	}
	
	public void paintComponent(Graphics g)
	{
		g.setColor(new Color(0, 0, 0));
		g.fillRect(0, 0, this.getWidth(), this.getHeight());
		g.setColor(new Color(255, 255, 255));
		g.fillRect(1, 1, this.getWidth() - 2, this.getHeight() - 2);
		
		int red = Integer.parseInt(Application.colour.substring(0, 3));
		int green = Integer.parseInt(Application.colour.substring(3, 6));
		int blue = Integer.parseInt(Application.colour.substring(6, 9));
		int alpha = Integer.parseInt(Application.colour.substring(9, 12));
		
		g.setColor(new Color(red, green, blue, alpha));
		g.fillRect(2, 2, this.getWidth() - 4, this.getHeight() - 4);
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		//Decipher current colour
		int red = Integer.parseInt(Application.colour.substring(0, 3));
		int green = Integer.parseInt(Application.colour.substring(3, 6));
		int blue = Integer.parseInt(Application.colour.substring(6, 9));
		int alpha = Integer.parseInt(Application.colour.substring(9, 12));
		Color pcolour = JColorChooser.showDialog(this, "Choose Pixel Colour", new Color(red, green, blue, alpha));
		if(pcolour == null)
		{
			return;
		}
		//Encode and write Colour
		String nred = Integer.toString(pcolour.getRed());
		while(nred.length() < 3)
		{
			nred = "0" + nred;
		}
		
		String ngreen = Integer.toString(pcolour.getGreen());
		while(ngreen.length() < 3)
		{
			ngreen = "0" + ngreen;
		}
		
		String nblue = Integer.toString(pcolour.getBlue());
		while(nblue.length() < 3)
		{
			nblue = "0" + nblue;
		}
		Application.colour = nred + ngreen + nblue + "255";
		this.repaint();
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
}